#include "csvformat.h"

using namespace std;
using namespace opsrc::stdf;


/*
 * File:   CsvFormat.cpp
 * Author: meng
 *
 * Created on May 5, 2011, 2:27 PM
 */

#include <sstream>

#include "csvformat.h"
#include "rectype.h"
#include "recbase.h"
#include "far.h"
#include "atr.h"
#include "mir.h"
#include "mrr.h"
#include "pcr.h"
#include "hbr.h"
#include "sdr.h"
#include "sbr.h"
#include "pmr.h"
#include "pgr.h"
#include "plr.h"
#include "rdr.h"
#include "wir.h"
#include "wrr.h"
#include "wcr.h"
#include "pir.h"
#include "prr.h"
#include "tsr.h"
#include "ptr.h"
#include "bps.h"
#include "eps.h"
#include "gdr.h"
#include "dtr.h"
#include "ftr.h"
#include "mpr.h"
#include <string>


#include "stdffield.h"
#include "stdffield.cpp"

using namespace std;
using namespace opsrc;
using namespace opsrc::stdf;

const string tab1 = "\t";
const string tab2 = "\t\t";
const string tab3 = "\t\t\t";


CsvFormat::CsvFormat() {
}


CsvFormat::~CsvFormat() {
}



//CsvFormat& CsvFormat::operator<< (RecBase* rec) {
void CsvFormat::out(RecBase* rec) {
    stdf_rec rt = rec->getStdfRec();
    switch (rt) {

    case ATR:
        {
            Atr* pAtr = dynamic_cast<Atr*>(rec);
            struct tm* t = Util::convertUnixTime2TM(pAtr->getModTime().getData());
            cout << asctime(t) << ","
                    << pAtr->getCmdLine().getData() << endl;
        }
        break;

    case BPS:
        {
            Bps* pBps = dynamic_cast<Bps*>(rec);
            cout << "BPS" << pBps->getSequenceName().getData() <<","<< pBps->getRecOrder() << endl;
        }
        break;

    case DTR:
        {
            Dtr* pDtr = dynamic_cast<Dtr*>(rec);
            cout << pDtr->getTextData().getData() << endl;
        }
        break;

    case EPS:
        {
            Eps* pEps = dynamic_cast<Eps*>(rec);
            cout << pEps->getRecOrder() << endl;
        }
        break;

    case FAR:
        {
            Far* pFar = dynamic_cast<Far*>(rec);
            (cout) << pFar->getStdfRec() << endl;
        }
        break;
    case FTR:
        {
            Ftr* p = dynamic_cast<Ftr*>(rec);
            (cout) << p->getTestNumber().getData() << ","
                    << p->getHeadNumber().getData() << ","
                    << p->getSiteNumber().getData() << ","
                    //<< p->getTestFlag().getData() << ","
                    << p->getVectorModulePatternName().getData() << ","
                    << p->getTimeSetName().getData() << ","
                    << p->getCycleCount().getData() << ","
                    << p->getRelativeVectorAddress().getData() << ","
                    << p->getRepeatedVectorCount().getData() << ","
                    << p->getFailPinBitMapLength().getData() << ","
                    << p->getXlogicalDeviceAddressFailure().getData() << ","
                    << p->getYlogicalDeviceAddressFailure().getData() << ","
                    << p->getVectorOffset().getData() << ","
                    //<< p->getPmrArrayData().getData() << ","
                    //<< p->getPmrArrayState().getData() << ","
                    //<< p->getProgramsStateIndexArray().getData() << ","
                    //<< p->getProgramStateDataArray().getData() << ","
                    << p->getFailPinBitMapLength().getData() << ","
                    //<< p->getOptionFlag().getData() << ","
                    << p->getTestNumber().getData() << ","
                    << p->getAlarmId().getData() << ","
                    << p->getAdditonalProgramInfo().getData() << ","
                    << p->getAdditionalResultInfo().getData() << ","
                    << p->getPatternGenertorNumber().getData() << ","
                    << p->getEnabledComparatorBitMap().getData() << endl;
        }
        break;

    case GDR:
        {
            //Gdr* pGdr = dynamic_cast<Gdr*>(rec);
            //cout<<"To be decide later."<<endl;
        }
        break;

    case HBR:
        {
            Hbr* pHbr = dynamic_cast<Hbr*>(rec);
            (cout) << pHbr->getHeadNum().getData() << ","
                    << pHbr->getSiteNum().getData() << ","
                    << pHbr->getHbinNum().getData() << ","
                    << pHbr->getHbinPassFail().getData() << ","
                    << pHbr->getHbinCount().getData() << ","
                    << pHbr->getHbinName().getData() << endl;
        }
        break;

    case MIR:
        {
            Mir* pMir = dynamic_cast<Mir*>(rec);
            (cout) << pMir->getJobName().getData() << ","
                    << pMir->getNodeName().getData() << ","
                    << pMir->getTesterType().getData() << ","
                    << Util::asciiTime( pMir->getSetupTime().getData()) << ","
                    << Util::asciiTime( pMir->getStartTime().getData()) << ","
                    << pMir->getOperatorName().getData() << ","
                    << pMir->getModeCode().getData() << ","
                    << pMir->getStartNum().getData() << ","
                    << pMir->getSubLotId().getData() << ","
                    << pMir->getPartType().getData() << ","
                    << pMir->getLotId().getData() << ","
                    << pMir->getTestCode().getData() << ","
                    << pMir->getRetestCode().getData() << ","
                    << pMir->getJobRevision().getData() << ","
                    << pMir->getExecutionType().getData() << ","
                    << pMir->getExecutiveVersion().getData() << ","
                    << pMir->getProductCode().getData() << ","
                    << pMir->getCmodCode().getData() << ","
                    << pMir->getBurnTime().getData() << ","
                    << pMir->getTestTemperature().getData() << ","
                    << pMir->getUserText().getData() << ","
                    << pMir->getAuxilaryFile().getData() << ","
                    << pMir->getPackageType().getData() << ","
                    << pMir->getFamilyId().getData() << ","
                    << pMir->getDateCode().getData() << ","
                    << pMir->getTestFacilityId().getData() << ","
                    << pMir->getTestFloorId().getData() << ","
                    << pMir->getProcessId().getData() << ","
                    << pMir->getOperationFrequency().getData() << ","
                    << pMir->getSpecName().getData() << ","
                    << pMir->getSpecVersion().getData() << ","
                    << pMir->getFlowId().getData() << ","
                    << pMir->getSetupId().getData() << ","
                    << pMir->getDesignId().getData() << ","
                    << pMir->getEngrgId().getData() << ","
                    << pMir->getRomCode().getData() << ","
                    << pMir->getSerialNumber().getData() << ","
                    << pMir->getSupervisorName().getData() << endl;
        }
        break;

    case MPR:
        {
            Mpr* pMpr = dynamic_cast<Mpr*>(rec);
            (cout) << pMpr->getTestNumber().getData() << ","
                    << pMpr->getHeadNumber().getData() << ","
                    << pMpr->getSiteNumber().getData() << ","
                    //<< pMpr->getReturnStatesArray().getData() << ","
                    << pMpr->getReturnResultsCount().getData() << ","
                    << pMpr->getUnits().getData() << ","
                    << pMpr->getLowerLimitFormat().getData() << ","
                    << pMpr->getTestUpperLimitValue().getData() << ","
                    << pMpr->getStartingInValue().getData() << ","
                    << pMpr->getStartingIncrementalValue().getData() << ","
                    << pMpr->getInputConditionUnits().getData() << ","
                    //<< pMpr->getPmrIndexArray().getData() << ","
                    << pMpr->getResultFormat().getData() << ","
                    << pMpr->getLowerLimitFormat().getData() << ","
                    << pMpr->getUpperLimitFormat().getData() << ","
                    << pMpr->getTestResultScale().getData() << ","
                    << pMpr->getTestLowerLimitScale().getData() << ","
                    << pMpr->getTestUpperLimitScale().getData() << ","
                    << pMpr->getLowerSpecLimit().getData() << ","
                    << pMpr->getUpperSpecLimit().getData() << "endl";
        }
        break;

    case MRR:
        {
            Mrr* pMrr = dynamic_cast<Mrr*>(rec);
            cout << Util::asciiTime(pMrr->getFinishTime().getData()) << ","
                    << pMrr->getDispCode().getData() << ","
                    << pMrr->getUserDescription().getData() << ","
                    << pMrr->getExecDescription().getData() << endl;
        }
        break;

    case PCR:
        {
            Pcr* pPcr = dynamic_cast<Pcr*>(rec);
            cout<< pPcr->getHeadNum().getData() << ","
                    << pPcr->getSiteNum().getData() << ","
                    << pPcr->getPartCount().getData() << ","
                    << pPcr->getAbortCount().getData() << ","
                    << pPcr->getRetestCount().getData() << ","
                    << pPcr->getGoodCount().getData() << ","
                    << pPcr->getFunctionalCount().getData() << endl;
        }
        break;

    case PGR:
        {
            Pgr* pPgr = dynamic_cast<Pgr*>(rec);
            cout << pPgr->getGroupIndex().getData() << ","
                    << pPgr->getGroupName().getData() << ","
                    << pPgr->getIndexCount().getData()
                    //<< pPgr->getPinIndexArray()
                    << endl;
        }
        break;

    case PIR:
        {
            Pir* pPir = dynamic_cast<Pir*>(rec);
            cout << pPir->getHeadNum().getData() << ","
                    << pPir->getSiteNum().getData() << endl;
        }
        break;

    case PLR:
        {
            Plr* pPlr = dynamic_cast<Plr*>(rec);
            cout << pPlr->getGroupCount().getData() << ","
                    //<< pPlr->getGroupIndex().getData() << ","
                    //<< pPlr->getGroupMode().getData() << ","
                    //<< pPlr->getGroupDisplayRadix().getData() << ","
                    //<< pPlr->getProgramStateEncodeChars().getData() << ","
                    //<< pPlr->getReturnStateStateEncodeChars().getData() << ","
                    //<< pPlr->getProgramStateEncodeChars().getData() << ","
                    //<< pPlr->getReturnStateStateEncodeCharsL().getData()
                    << endl;
        }
        break;

    case PMR:
        {
            Pmr* p = dynamic_cast<Pmr*>(rec);
            cout << p->getSiteNum().getData() << ","
                    << p->getUniquePinIndex().getData() << ","
                    << p->getChannelType().getData() << ","
                    << p->getChannelName().getData() << ","
                    << p->getPhysicalPinName().getData() << ","
                    << p->getLogicalPinName().getData() << ","
                    << p->getHeadNum().getData() << ","<<endl;
        }
        break;

    case PRR:
        {
            Prr* pPrr = dynamic_cast<Prr*>(rec);
            cout << pPrr->getHeadNumber().getData() << ","
                    << pPrr->getSiteNumber().getData() << ","
                    << pPrr->getPartId().getData() << ","
                    << pPrr->getNumberOfTests().getData() << ","
                    //<< pPrr->getPartFlag().getData() << ","
                    << pPrr->getHardBin().getData() << ","
                    << pPrr->getSoftBin().getData() << ","
                    << pPrr->getXCoordinate().getData() << ","
                    << pPrr->getYCoordinate().getData() <<","
                    << pPrr->getTestTime().getData() << endl;
        }
        break;

    case PTR:
        {
            Ptr* p = dynamic_cast<Ptr*>(rec);
            cout << p->getTestNumber().getData() << ","
                    << p->getHeadNumber().getData() << ","
                    << p->getSiteNumber().getData() << ","
                    //<< p->getTestFlag().getData() << ","
                    //<< p->getParaTestFlag().getData() << ","
                    << p->getResult().getData() << ","
                    //<< p->getTestFlag().getData() << ","
                    << p->getUpperLimitScale().getData() << ","
                    << p->getTestText().getData() << ","
                    //<< p->getOptionFlag().getData()<< ","
                    << p->getResultScale().getData() << ","
                    << p->getLowerLimitScale().getData() << ","
                    << p->getUpperLimitScaleFormat().getData() << ","
                    << p->getLowerTestLimitValue().getData() << ","
                    << p->getUpperTestLimitValue().getData() << ","
                    << p->getResultsUnit().getData() << ","
                    << p->getResultFormat().getData() << ","
                    << p->getLowerLimitScaleFormat().getData() << ","
                    << p->getUpperLimitScaleFormat().getData() << ","
                    << p->getLowerLimitSpec().getData() << endl;
        }
        break;

    case RDR:
        {
            Rdr* pRdr = dynamic_cast<Rdr*>(rec);
            cout<< pRdr->getRetestBinCount().getData() << endl;
            //<< pRdr->getRetestBinNumber().getData()<< endl;
        }
        break;

    case SBR:
        {
            Sbr* pSbr = dynamic_cast<Sbr*>(rec);
            cout << pSbr->getHeadNum().getData() << ","
                    << pSbr->getSiteNum().getData() << ","
                    << pSbr->getSbinNum().getData() << ","
                    << pSbr->getSbinCount().getData() << ","
                    << pSbr->getSbinPassFail().getData() << ","
                    << pSbr->getSbinName().getData() << endl;
        }
        break;

    case SDR:
        {
            Sdr* pSdr = dynamic_cast<Sdr*>(rec);
            cout<< pSdr->getHeadNum().getData() << ","
                    << pSdr->getSiteGroup().getData() << ","
                    //<< pSdr->getSiteNum().getData() << ","
                    << pSdr->getHandlerType().getData() << ","
                    << pSdr->getHandlerId().getData() << ","
                    << pSdr->getProbeCardType().getData() << ","
                    << pSdr->getProbeCardId().getData() << ","
                    << pSdr->getLoadBoardType().getData() << ","
                    << pSdr->getLoadBoardId().getData() << ","
                    << pSdr->getDibBoardType().getData() << ","
                    << pSdr->getDibBoardId().getData() << ","
                    << pSdr->getCableType().getData() << ","
                    << pSdr->getCableId().getData() << ","
                    << pSdr->getContactorType().getData() << ","
                    << pSdr->getExtraEqptType().getData() << ","
                    << pSdr->getContactorId().getData() << ","
                    << pSdr->getLaserType().getData() << ","
                    << pSdr->getLaserId().getData() << ","
                    << pSdr->getExtraEqptId().getData() << endl;
        }
        break;

    case TSR:
        {
            Tsr* pTsr = dynamic_cast<Tsr*>(rec);
            cout << pTsr->getHeadNumber().getData() << ","
                    << pTsr->getSiteNumber().getData() << ","
                    << pTsr->getTestType().getData() << ","
                    << pTsr->getTestNumber().getData() << ","
                    << pTsr->getExecutionCount().getData() << ","
                    << pTsr->getFailureCount().getData() << ","
                    << pTsr->getAlarmCount().getData() << ","
                    << pTsr->getTestName().getData() << ","
                    << pTsr->getSequenceName().getData() << ","
                    << pTsr->getTestLabel().getData() << ","
                    //<< pTsr->getOptionFlag().getData() << ","
                    << pTsr->getTestTime().getData() << ","
                    << pTsr->getLowestTestValue().getData() << ","
                    << pTsr->getHighestTestValue().getData() << ","
                    << pTsr->getTestResultSumValue().getData() << ","
                    << pTsr->getTestResultSquareValue().getData() << endl;
        }
        break;

    case WCR:
        {
            Wcr* pWcr = dynamic_cast<Wcr*>(rec);
            cout << pWcr->getWaferSize().getData() << ","
                    << pWcr->getDieHeight().getData() << ","
                    << pWcr->getDieWidth().getData() << ","
                    << pWcr->getWaferFlat().getData() << ","
                    << pWcr->getCenterX().getData() << ","
                    << pWcr->getCenterY().getData() << ","
                    << pWcr->getPositionX().getData() << ","
                    << pWcr->getPositionY().getData() << ","
                    << pWcr->getWaferUnit().getData() << endl;
        }
        break;

    case WIR:
        {
            Wir* p = dynamic_cast<Wir*>(rec);
            cout << p->getHeadNum().getData() << ","
                    << p->getSiteGroup().getData() << ","
                    << Util::asciiTime( p->getStartTime().getData() ) << ","
                    << p->getWaferId().getData() << endl;
        }
        break;

    case WRR:
        {
            Wrr* pWrr = dynamic_cast<Wrr*>(rec);
            cout << pWrr->getHeadNum().getData() << ","
                    << pWrr->getSiteGroup().getData() << ","
                    << pWrr->getPartCount().getData() << ","
                    << Util::asciiTime( pWrr->getFinishTime().getData()) << ","
                    << pWrr->getRetestCount().getData() << ","
                    << pWrr->getAbortCount().getData() << ","
                    << pWrr->getGoodCount().getData() << ","
                    << pWrr->getFunctionalCount().getData() << ","
                    << pWrr->getWaferId().getData() << ","
                    << pWrr->getFabWaferId().getData() << ","
                    << pWrr->getFrameId().getData() << ","
                    << pWrr->getMaskId().getData() << ","
                    << pWrr->getUserDescription().getData() << ","
                    << pWrr->getExecDescription().getData() << endl;
        }
        break;

    case UNK:
        // do nothing
        break;

    default:
        break;

    }
}

void CsvFormat::printHeaderInfo(RecBase* rec) {
    cout << tab1 << "rec-order : " << rec->getRecOrder() << endl;
    cout << tab1 << "file-byte-start-position : " << rec->getFileRecByteStartPosition() << endl;
    cout << tab1 << "file-byte-end-position : " << ( rec->getFileRecByteStartPosition() + rec->getRecByteCount() + 4 -1 )  << endl;
    cout << tab1 << "rec-total-byte-count : " << (rec->getRecByteCount() + 4) << endl;
    cout << tab1 << "rec-data-byte-count : " << rec->getRecByteCount() << endl;

}


void CsvFormat::printFieldInfo(const char* name, StdfField<unsigned int>& f, long stdfRecStartBytePosition) {
    string nm(name);
    if (f.getTotalByteCount()== 0) {
        cout << tab1 << nm << " (nil) " << endl;
        return;
    }
    cout << tab1 << nm << endl;
    cout << tab2 << "file-byte-start-position : " << (stdfRecStartBytePosition + 4 +  f.getOffset() ) << endl;
    cout << tab2 << "offset : " << f.getOffset() << endl;
    cout << tab2 << "total-byte-count : " << f.getTotalByteCount() << endl;
    cout << tab2 << "data-byte-count : " << f.getByteCount() << endl;
    cout << tab2 << "data-value : " << f.getData() << endl;

}

void CsvFormat::printFieldInfo(const char* name, StdfField<int>& f, long stdfRecStartBytePosition) {
    string nm(name);
    if (f.getTotalByteCount()== 0) {
        cout << tab1 << nm << " (nil) " << endl;
        return;
    }
    cout << tab1 << nm << endl;
    cout << tab2 << "file-byte-start-position : " << (stdfRecStartBytePosition + 4 +  f.getOffset() ) << endl;
    cout << tab2 << "offset : " << f.getOffset() << endl;
    cout << tab2 << "total-byte-count : " << f.getTotalByteCount() << endl;
    cout << tab2 << "data-byte-count : " << f.getByteCount() << endl;
    cout << tab2 << "data-value : " << f.getData() << endl;
}


void CsvFormat::printFieldInfo(const char* name, StdfField<string>& f, long stdfRecStartBytePosition) {
    string nm(name);
    if (f.getTotalByteCount()== 0) {
        cout << tab1 << nm << " (nil) " << endl;
        return;
    }
    cout << tab1 << nm << endl;
    cout << tab2 << "file-byte-start-position : " << (stdfRecStartBytePosition + 4 +  f.getOffset() ) << endl;
    cout << tab2 << "offset : " << f.getOffset() << endl;
    cout << tab2 << "total-byte-count : " << f.getTotalByteCount() << endl;
    cout << tab2 << "data-byte-count : " << f.getByteCount() << endl;
    cout << tab2 << "data-value : " << f.getData() << endl;
}

void CsvFormat::printFieldInfo(const char* name, StdfField<long>& f, long stdfRecStartBytePosition) {
    string nm(name);
    if (f.getTotalByteCount()== 0) {
        cout << tab1 << nm << " (nil) " << endl;
        return;
    }
    cout << tab1 << nm << endl;
    cout << tab2 << "file-byte-start-position : " << (stdfRecStartBytePosition + 4 +  f.getOffset() ) << endl;
    cout << tab2 << "offset : " << f.getOffset() << endl;
    cout << tab2 << "total-byte-count : " << f.getTotalByteCount() << endl;
    cout << tab2 << "data-byte-count : " << f.getByteCount() << endl;
    cout << tab2 << "data-value : " << f.getData() << endl;
}


void CsvFormat::out(string& msg) {
    cout << msg  ;
}

void CsvFormat::out(const char* msg) {

    cout << msg  ;
}

void CsvFormat::out(int value) {
    std::stringstream out;
    out << value;
    cout << out.str() ;
}

void CsvFormat::out(float value) {
    std::stringstream out;
    out << value;
    cout << out.str() ;
}

void CsvFormat::out(long value) {
    std::stringstream out;
    out << value;
    cout << out.str() ;
}

void CsvFormat::open() {

}

void CsvFormat::close() {

}


void CsvFormat::printFieldInfo(const char* name, StdfField<vector<bool> >& f, long stdfRecStartBytePosition) {
    string nm(name);
    if (f.getTotalByteCount()== 0) {
        cout << tab1 << nm << " (nil) " << endl;
        return;
    }

    vector<bool> arr = f.getData();
    string data = "";
    std::stringstream out;

    if ((int)arr.size()>0) {

        vector<bool>::const_iterator it;
        for (it=arr.begin(); it!=arr.end();it++) {
            out << (*it? 1: 0 );
            if (it != arr.end()-1)
               out << ",";
        }
        data += out.str();
    }



    cout << tab1 << nm << endl;
    cout << tab2 << "file-byte-start-position : " << (stdfRecStartBytePosition + 4 +  f.getOffset() ) << endl;
    cout << tab2 << "offset : " << f.getOffset() << endl;
    cout << tab2 << "total-byte-count : " << f.getTotalByteCount() << endl;
    cout << tab2 << "data-byte-count : " << f.getByteCount() << endl;
    cout << tab2 << "data-value : " << data << endl;
}



void CsvFormat::printFieldInfo(const char* name, StdfField<vector<int> >& f, long stdfRecStartBytePosition) {
    string nm(name);
    if (f.getTotalByteCount()== 0) {
        cout << tab1 << nm << " (nil) " << endl;
        return;
    }

    vector<int> arr = f.getData();
    string data = "";
    std::stringstream out;

    if ((int)arr.size()>0) {

        vector<int>::const_iterator it;
        for (it=arr.begin(); it!=arr.end();it++) {
            out << *it;
            if (it != arr.end()-1)
               out << ",";
        }
        data += out.str();
    }

    cout << tab1 << nm << endl;
    cout << tab2 << "file-byte-start-position : " << (stdfRecStartBytePosition + 4 +  f.getOffset() ) << endl;
    cout << tab2 << "offset : " << f.getOffset() << endl;
    cout << tab2 << "total-byte-count : " << f.getTotalByteCount() << endl;
    cout << tab2 << "data-byte-count : " << f.getByteCount() << endl;
    cout << tab2 << "data-value : " << data << endl;
}

void CsvFormat::printFieldInfo(const char* name, StdfField<CharArray>& f, long stdfRecStartBytePosition) {
    string nm(name);
    if (f.getTotalByteCount()== 0) {
        cout << tab1 << nm << " (nil) " << endl;
        return;
    }

    string data = "";

    cout << tab1 << nm << endl;
    cout << tab2 << "file-byte-start-position : " << (stdfRecStartBytePosition + 4 +  f.getOffset() ) << endl;
    cout << tab2 << "offset : " << f.getOffset() << endl;
    cout << tab2 << "total-byte-count : " << f.getTotalByteCount() << endl;
    cout << tab2 << "data-byte-count : " << f.getByteCount() << endl;
    cout << tab2 << "data-value : " << data << endl;
}

